package com.dashu.device.service.HF;


import com.dashu.base.bean.device.HF.req.HFReqCompared;
import com.dashu.base.bean.device.HF.req.HFReqHeartbeat;
import com.dashu.base.bean.device.HF.req.HFReqOnline;
import com.dashu.base.bean.device.HF.resp.HFRespCompared;
import com.dashu.base.bean.device.HF.resp.HFRespHeartbeat;
import com.dashu.base.bean.device.HF.resp.HFRespOnline;
import com.dashu.base.redis.JedisUtil;
import com.dashu.base.redis.RedisLiveKeyConstants;
import com.dashu.base.utils.CopyUtil;
import com.dashu.base.utils.ToEntityAnStr;
import com.dashu.base.utils.UIDUtil;
import com.dashu.device.api.HF.HFInsideServerApi;
import com.dashu.device.modelat.Distinguish;
import com.dashu.device.service.*;
import com.dashu.device.util.CQWriteUtil;
import com.dashu.device.util.HFWriteUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static com.dashu.device.container.HFContainer.HFonlineStr;
import static com.dashu.device.tools.HFMark.*;

@Slf4j
@Service
public class HFnsideInsideService<T> extends AttendanceBaseServer implements HFInsideServerApi<T> {


    private static HFnsideInsideService hFnsideInsideService; // 2. 定义本类的静态对象

    @PostConstruct // 3. 添加 @PostConstruct 注解的方法
    public void init() {
        hFnsideInsideService = this;
    }

    @Autowired
    private DeviceService deviceService;

    /**
     * 设备注册
     */
    @Override
    public void register(ChannelHandlerContext ctx, HFReqOnline req) {
        HFRespOnline hfRespOnline = new HFRespOnline();
        hfRespOnline.setResp_type(req.getRequest_type());
        hfRespOnline.setCode(0);
        hfRespOnline.setDevice_id(req.getDevice_id());
        hfRespOnline.setLog(HF_RESP_ONLINE);
        HFWriteUtil.send(ctx, ToEntityAnStr.obj2String(hfRespOnline));
    }

    /**
     * 设备心跳
     */
    @Override
    public void heartbeat(ChannelHandlerContext ctx, Object object, long cacheTime) {
        HFReqHeartbeat hfReqHeartbeat = CopyUtil.copyEnty(object, HFReqHeartbeat.class);
        //设备心跳
        JedisUtil.hmset(RedisLiveKeyConstants.ON_LINE, hfReqHeartbeat.getDevice_id(), String.valueOf(cacheTime));
        HFonlineStr.put(hfReqHeartbeat.getDevice_id(), ctx);
        hFnsideInsideService.deviceService.updataByDeviceUUId(hfReqHeartbeat.getDevice_id(), hfReqHeartbeat.getIP());
        HFRespHeartbeat hfRespHeartbeat = new HFRespHeartbeat();
        hfRespHeartbeat.setResp_type(hfReqHeartbeat.getRequest_type());
        hfRespHeartbeat.setCode(0);
        hfRespHeartbeat.setLog(HF_RESP_HEARD);
        HFWriteUtil.send(ctx, ToEntityAnStr.obj2String(hfRespHeartbeat));
    }


    @Override
    public void compared(ChannelHandlerContext ctx, Object object, String msg) throws IOException {
        log.info("" + ctx.channel().remoteAddress().toString() + "--compared");
        HFReqCompared hfReqCompared = CopyUtil.copyEnty(object, HFReqCompared.class);
        Distinguish distinguish = new Distinguish();
        List<String> pg = Arrays.asList(hfReqCompared.getUser_id().split("_"));
        if (pg.size() > 1) {
            distinguish.setId(UIDUtil.nextId());
            distinguish.setName(hfReqCompared.getUser_name());
            distinguish.setIden(pg.get(0));
            distinguish.setCId(Long.valueOf(pg.get(1)));
            String filetype = hfReqCompared.getFrpic_name().substring(hfReqCompared.getFrpic_name().indexOf("."));
            distinguish.setImg(imgRecord(hfReqCompared.getImage(), pg.get(1) + filetype));
            distinguish.setCaptureTime(hfReqCompared.getTimestamp()*1000);
            distinguish.setCompareResult(Double.parseDouble(String.valueOf(hfReqCompared.getComp_score()))/100);
            distinguish.setUuid(hfReqCompared.getDevice_id());
            distinguish.setUuid(hfReqCompared.getDevice_id());
            if (hfReqCompared.getUser_temperature() != null)
                distinguish.setTemperature(Double.parseDouble(hfReqCompared.getUser_temperature()));
            isAttendance(distinguish);
        }

        HFRespCompared comparedBaseResp = new HFRespCompared();
        comparedBaseResp.setCode(0);
        if (hfReqCompared != null) {
            comparedBaseResp.setResp_type(hfReqCompared.getRequest_type());
            comparedBaseResp.setFrpic_name(hfReqCompared.getFrpic_name());
        }
        comparedBaseResp.setLog(HF_RESP_COMPARED);
        CQWriteUtil.send(ctx, ToEntityAnStr.obj2String(comparedBaseResp));
    }


}
